package ooday04;

import java.util.concurrent.Callable;

public class Test {
    public static void main(String[] args) {
        //演示向上造型(多态)的第1点应用:
        //Animal o = new Animal(); //抽象类不能被实例化
        Animal[] animals=new Animal[5];//创建Animal数组对象
        animals[0]=new Dog("小黑",2,"黑");//向上造型
        animals[1]=new Dog("小白",3,"白");
        animals[2]=new Fish("小金",1,"金");
        animals[3]=new Fish("小红",4,"红");
        animals[4]=new Chick("小黄",2,"黄");
        for (int i=0;i<animals.length;i++){//遍历所有动物
            animals[i].eat();//每个动物吃饭
            animals[i].drink();//每个动物喝水
            if (animals[i] instanceof Dog){//若为Dog类型
                Dog dog=(Dog) animals[i];
                dog.lookHome();
            }
            if (animals[i] instanceof Swim){//若能游泳(实现Swim接口的)
                Swim s=(Swim) animals[i];
                s.swim();
            }
            if (animals[i] instanceof Chick){//若为Chick类型
                Chick chick=(Chick) animals[i];
                chick.layEggs();
            }
            }





















        //演示向上造型(多态)的第2点应用:
        Master master=new Master();
        Dog dog=new Dog("小黑",2,"黑");
        Fish fish=new Fish("小金",1,"金");
        Chick chick=new Chick("小红",3,"红");
        master.feed(dog);//在传参的同时，系统自动做了向上造型
        master.feed(fish);
        master.feed(chick);


    }
}
